package com.zzy.threaddemo.newdemo;

import java.util.concurrent.Callable;

import com.google.common.util.concurrent.RateLimiter;

public class CallableTask implements Callable<String> {
	final RateLimiter limiter = RateLimiter.create(2); // 每秒不超过2个任务被提交
	
	private  String theadName;  //线程名

	public CallableTask(String theadName) {
		super();
		this.theadName = theadName;
	}


	public String call() throws Exception {
		int abc = 0;
		for (int i = 0; i < 10; i++) {
			Thread.sleep(10);
			abc++;
			System.out.println(theadName+"count---"+abc);
			
			
					// limiter.acquire(); //请求RateLimiter, 超过permits会被阻塞
					if (limiter.tryAcquire()) { // 未请求到limiter则立即返回false
						System.out.println("没有超过" + limiter.getRate());
					} else {
						System.out.println("超过" + limiter.getRate());
					}
			
			
		}
		return abc+"";
		// 递归调用
	}

}
